/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kelaite.procurement.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author algol
 */
public class FileUpload {

    // 运行前先设置好以下三个参数
    private static final String BUCKET_NAME = "w1buy";
    private static final String USER_NAME = "w1buy";
    private static final String USER_PWD = "w1buytest";

    /**
     * 绑定的域名
     */
    private static final String URL = "http://w1buy.b0.upaiyun.com";
    /**
     * 根目录
     */
    private static final String DIR_ROOT = "/";

    private static UpYun upyun = null;

    static {
        // 初始化空间
        upyun = new UpYun(BUCKET_NAME, USER_NAME, USER_PWD);
    }

    public static synchronized String writeFile(MultipartFile file, String targetFileName) {
        String url = null;
        try {
            String filePath = DIR_ROOT + targetFileName;
            upyun.setContentMD5(upyun.md5(file));
            boolean result = upyun.writeFile(filePath, file, true);
            if (result == true) {
                url = URL + "/" + targetFileName;
            }
        } catch (IOException ex) {
            Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
        }
        return url;
    }

    public static boolean uploadThum() {
        return false;
    }

    public static void main(String[] args) throws IOException {
        String t = "C:\\Users\\adminitrator\\Pictures\\afd.jpg";
        File file = new File(t);
        //String ret = writeFile(file, "a.jpeg");
       // System.err.println(ret);
    }
}
